package com.tianxiabuyi.model.base;

import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;
import com.tianxiabuyi.model.pojo.User;
import com.tianxiabuyi.utils.JSONUtil;
import com.tianxiabuyi.utils.Tools;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 数据模型最顶层父类<br>
 * @author yangh
 */
@MappedSuperclass
public class TXBYObject implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;               // 流水号
    private Integer hospital;         // 医院或科室id
    private Integer status;           // 状态
    @Column(name = "create_time") @JSONField(name = "create_time")
    private Long cTime;               // 创建时间戳
    @Column(name= "json")
    private String jsonString = "{}"; // 其它备用字段
    @Transient
    private Map<String, Object> json = new HashMap<String, Object>();

    public TXBYObject() {
        this.cTime = System.currentTimeMillis();
        this.status = 1;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getHospital() {
        return hospital;
    }

    public void setHospital(Integer hospital) {
        this.hospital = hospital;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getcTime() {
        return cTime;
    }

    public void setcTime(Long cTime) {
        this.cTime = cTime;
    }

    public String getJsonString() {
        return jsonString;
    }

    public void setJsonString(String jsonString) {
        try {
            this.json = JSONUtil.fromJson(jsonString, Map.class);
        } catch (Exception e) {
            throw new RuntimeException("请赋值合法json字符串");
        }
        this.jsonString = jsonString;
    }

    public Map<String, Object> getJson() {
        modJson();
        this.jsonString = JSONUtil.toJson(json);
        return json;
    }

    /**
     * 根据属性名称取值
     */
    public Object getJson(String key) {
        modJson();
        return this.json.get(key);
    }

    public void setJson(Map<String, Object> json) {
        this.jsonString = JSONUtil.toJson(json);
        this.json = json;
    }

    /**
     * 添加一个属性和值
     */
    public void setJson(String key, Object value) {
        modJson();
        this.json.put(key, value);
        this.jsonString = JSONUtil.toJson(json);
    }

    /**
     * 批量添加属性和值
     */
    public void addJson(Map<String, Object> anJson) {
        modJson();
        this.json.putAll(anJson);
        this.jsonString = JSONUtil.toJson(json);
    }

    /**
     * 动态改变jsonString的内容
     */
    private void modJson() {
        Map<String, Object> map = JSONUtil.fromJson(jsonString, Map.class);
        this.setJson(map);
    }

    /**
     * 对象转JSON
     */
    public String toJson() {
        return JSONUtil.toJson(this);
    }

    /**
     * 对象转map
     */
    @SuppressWarnings("rawtypes")
    public Map toMap() {
        return Tools.toMap(this);
    }

    /**
     * 通过JSON生成对象
     * @param json 要生成对象的JSON字符串
     * @param c 真实的对象class
     * @return 生成的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T fromJson(String json, Class<?> c) {
        Map<String, Object> origMap =  JSONUtil.fromJson(json, new TypeReference<Map<String, Object>>(){});
        if (null == origMap) {
            origMap = new HashMap<String, Object>();
        }
        TXBYObject txbyObject = (TXBYObject)JSONUtil.fromJson(json, c);
        if (null == txbyObject) {
            try {
                txbyObject = (TXBYObject)c.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        Map<String, Object> map = (Map<String, Object>)origMap.get("json");//GsonUtil.fromJson(txbyObject.getJsonString(), new TypeToken<Map<String, Object>>(){});
        if (null == map) {
            map = new HashMap<String, Object>();
        }
        if (txbyObject instanceof User) {
            map.put("from", Tools.getDesc(origMap.get("from"), ""));
            map.put("token", Tools.getDesc(origMap.get("token"), ""));
            map.put("ip", Tools.getDesc(origMap.get("ip"), ""));
        }
        txbyObject.setJson(map);
        return (T)txbyObject;
    }

    /**
     * 通过容器map生成对象
     * @param map 要生成对象的map容器
     * @param c 真实的对象class
     * @return 生成的对象
     */
    @SuppressWarnings("rawtypes")
    public static <T> T fromMap(Map map, Class<?> c) {
        String json = JSONUtil.toJson(map);
        return fromJson(json, c);
    }

}